import { addTopo, delTopo } from "@/api/modules/scada/service";
import { IS_API_PROXY, SERVERS } from "@/config/config";
import { PublicStore } from "@/stores/modules/public";
import { openWin } from "@/utils/routerUtil";
import { matchIp } from "@/utils/str";
import { ResultData } from "./../api/interface/index";
const topoTemplate = {
	x: 0,
	y: 213,
	scale: 0.4059662839165818,
	pens: [
		{
			text: "",
			width: 741,
			height: 441.52080232381763,
			name: "square",
			alias: "画布",
			id: "z00310",
			x: 0,
			y: 0,
			lineWidth: 0,
			imageRatio: false,
			locked: 10,
			isBottom: true,
			coverage: [{ name: "背景", status: { look: true }, index: 0, number: 0 }],
			fontSize: 12,
			lineHeight: 1.5,
			anchors: [
				{ id: "0", penId: "z00310", x: 0.5, y: 0 },
				{ id: "1", penId: "z00310", x: 1, y: 0.5 },
				{ id: "2", penId: "z00310", x: 0.5, y: 1 },
				{ id: "3", penId: "z00310", x: 0, y: 0.5 }
			],
			ex: 741,
			ey: 441.52080232381763,
			center: { x: 370.5, y: 220.76040116190882 },
			station: ["root"],
			dev: ["2345499854798379"]
		}
	],
	origin: { x: 0, y: 0 },
	center: { x: 0, y: 0 },
	paths: {},
	rule: true,
	version: "1.0.0"
};
export const TopoType = {
	device: "device",
	product: "product",
	business: "business"
};
export interface TopoInfo {
	id: string;
	cover: string;
	defaultCover: string;
	label: string;
	data: string;
	dataDraft: string;
	mobileData: string;
	mobileDataDraft: string;
	sysComp: string;
	parentId: string;
	creatorId: string;
	creator: string;
	agencyId: string;
	deptId: string;
	//扩展属性，添加设备组态，将devSn赋值给sourceId
	devSn?: string;
	//扩展属性，添加设备组态，将productModel赋值给sourceId
	productModel?: string;
	remark: string;
	createTime: string;
	updateTime: string;
	sourceId?: string;
	sourceType: "device" | "product" | "business";

	// [key: string]: any;
}
export const useTopo = () => {
	const getTopoServer = () => {
		let hostname = location.hostname;
		let server;
		if (hostname == "localhost") {
			// server = "http://localhost:1818";
			server = "http://172.16.22.81:9000/scada";
			// server = "http://ssiot.cc/scada";
			// server = import.meta.env[`VITE_SERVER_${SERVERS.scada.name.toUpperCase()}`] + `/${SERVERS.scada.name}`;
		} else if (IS_API_PROXY) {
			server = "/" + SERVERS.scada.name;
		} else if (matchIp(hostname)) {
			server = location.port
				? location.origin.replace(location.port, SERVERS.scada.port + "")
				: location.origin + `:${SERVERS.scada.port}`;
		} else {
			//插入前缀
			server = location.origin.replace(location.protocol + "//", location.protocol + `/${SERVERS.scada.apiPrefix}.`);
			//替换端口
			server = location.port ? server.replace(location.port, SERVERS.ssiot.port + "") : server + `:${SERVERS.ssiot.port}`;
		}

		return server;
	};
	//组态编辑器
	const gotoTopoEditer = (topoId: string) => {
		let url = `${getTopoServer()}/topo/#/visual/edit?id=${topoId}&token=` + PublicStore().token;
		openWin(url);
	};

	const addTopoInfo = async ({
		label,
		deptId,
		sourceId,
		sourceType
	}: {
		label: string;
		deptId: string;
		sourceId: string;
		sourceType: string;
	}): Promise<ResultData> => {
		let config = JSON.parse(JSON.stringify(topoTemplate));
		config.pens[0].station = [deptId];
		config.pens[0].dev = [sourceId];
		let param = {
			label: label || sourceId || "我的组态",
			deptId: deptId,
			sourceId: sourceId,
			sourceType: sourceType,
			data: JSON.stringify(config),
			dataDraft: JSON.stringify(config)
		};
		return addTopo(param);
	};
	const delTopoInfo = (topo: { id: string }) => {
		return delTopo(topo);
	};
	return { getTopoServer, addTopoInfo, delTopoInfo, gotoTopoEditer };
};
